#include "slicenode.h"
#include <osg/LineWidth>

namespace HIM {
SliceNode::SliceNode():
    sliceNode_{new osg::Geode},
    tr_ {new osg::MatrixTransform},
    sliceGeom_{new osg::Geometry}
{
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();

    sliceGeom_->setUseDisplayList(false);
    sliceGeom_->setUseVertexBufferObjects(true);
    sliceGeom_->setDataVariance(osg::Object::DYNAMIC);
    sliceGeom_->setVertexArray(vertices);//设置点集
    sliceGeom_->setColorArray(colors);//设置颜色
    sliceGeom_->setColorBinding(osg::Geometry::BIND_PER_VERTEX);//OVERALL表示所有的点都一个颜色，BIND_PER_VERTEX表示每个点各有一种颜色
    sliceGeom_->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size()));
    setLineWidth(1);
    osg::Geode* geo = new osg::Geode;
    geo->addDrawable(sliceGeom_);
    tr_->addChild(geo);
    sliceNode_->addChild(tr_.get());
}

void SliceNode::setLineWidth(float Width)
{
    osg::ref_ptr<osg::LineWidth> lineWidth = new osg::LineWidth(Width);
    sliceGeom_->getOrCreateStateSet()->setAttribute(lineWidth);
}

void SliceNode::updataSliceNode(float zPos ,const QMap<int,QVector<QPair<QPointF,QPointF>>>& lineList)
{
    osg::Vec3Array* vertices_ = dynamic_cast<osg::Vec3Array*>(sliceGeom_->getVertexArray());
    vertices_->clear();
    osg::Vec4Array* colors_ = dynamic_cast<osg::Vec4Array*>(sliceGeom_->getColorArray());
    colors_->clear();

    for (auto it = lineList.constBegin(); it != lineList.constEnd(); ++it) {
        for(const auto pair : it.value()){
            osg::Vec3d pointStart;
            osg::Vec3d pointEnd;
            pointStart.x() = pair.first.x();
            pointStart.y() = pair.first.y();
            pointStart.z() = zPos;
            vertices_->push_back(pointStart);
            colors_->push_back(colorRing[it.key()]);

            pointEnd.x() = pair.second.x();
            pointEnd.y() = pair.second.y();
            pointEnd.z() = zPos;
            vertices_->push_back(pointEnd);

            colors_->push_back(colorRing[it.key()]);
        }
    }
    //获取DrawArrays
    dynamic_cast<osg::DrawArrays*>(sliceGeom_->getPrimitiveSet(0))->setCount(vertices_->size());
    vertices_->dirty();
    colors_->dirty();
}

void SliceNode::clearSliceNode()
{
    osg::Vec3Array* vertices_ = dynamic_cast<osg::Vec3Array*>(sliceGeom_->getVertexArray());
    vertices_->clear();
    osg::Vec4Array* colors_ = dynamic_cast<osg::Vec4Array*>(sliceGeom_->getColorArray());
    colors_->clear();
    //获取DrawArrays
    dynamic_cast<osg::DrawArrays*>(sliceGeom_->getPrimitiveSet(0))->setCount(0);
    vertices_->dirty();
    colors_->dirty();
}

void SliceNode::moveSliceNode(float x,float y,float z)
{
    tr_->setMatrix(osg::Matrix::translate(x,y,z));
}

void SliceNode::addToSliceNode()
{
//    osg::Vec3Array* vertices_ = dynamic_cast<osg::Vec3Array*>(sliceGeom_->getVertexArray());
//    osg::Vec4Array* colors_ = dynamic_cast<osg::Vec4Array*>(sliceGeom_->getColorArray());
//    for(auto it: points)
//    {
//        osg::Vec3d point;
//        point.x() = it.x();
//        point.y() = it.y();
//        point.z() = it.z();
//        //Points_->push_back(point);
//        vertices_->push_back(point);
//        colors_->push_back(osg::Vec4(((500-point.z())/500)*1, 1-((500-point.z())/500), 0.1, 0.5));
//    }

//    //获取DrawArrays
//    dynamic_cast<osg::DrawArrays*>(sliceGeom_->getPrimitiveSet(0))->setCount(vertices_->size());
//    vertices_->dirty();
//    colors_->dirty();
}
}
